const app = getApp();
var sourceType = [ ['camera'], ['album'], ['camera', 'album'] ];
var sizeType = [ ['compressed'], ['original'], ['compressed', 'original'] ];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    IDCard:'',
    imgs:[{tishi:'点击拍摄/上传人像面',isupload:false,img:'','imgl':''},{tishi:'点击拍摄/上传国徽面',isupload:false,img:'','imgl':''},{tishi:'点击上传手持身份证照片',isupload:false,img:'','imgl':''}],
    IsUpdate:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.FJ_Get_IDCardZjInfo()
  },
// UID：登录人的用户ID  type:1身份信息 2证件资料
// type=1时： IDCard：身份证号  IDCardFront：身份证正面  IDCardBack:身份证背面,IDCardHand:手持身份证
  addJbxx:function () {
    var that = this;
    var FjLoginLog = wx.getStorageSync('FjLoginLog'); 
    if (this.data.IDCard.length < 2) {
      app.tishi('请输入您的身份证号');return false;
    }
    var IDCardImg = this.data.imgs;
    if (!IDCardImg[0].isupload) {
      app.tishi('请拍摄/上传人像面');return false;
    }
    if (!IDCardImg[1].isupload) {
      app.tishi('请拍摄/上传国徽面');return false;
    }  
    if (!IDCardImg[2].isupload) {
      app.tishi('请上传手持身份证照片');return false;
    }
    var api_input = {
      UID:FjLoginLog.UID,
      type:1,
      IDCard:this.data.IDCard,
      IDCardFront:IDCardImg[0].img,
      IDCardBack:IDCardImg[1].img,
      IDCardHand:IDCardImg[2].img
    }
    app.getDaoPostN('api/FJDispose/FJ_Edit_IDCardZjInfo',api_input,r=>{
        
        if (r.code==200) {
          app.tishi(r.data);
          setTimeout(function () {
            wx.navigateBack()
          },1000)
        }else{
          app.tishi(r.data);
        }
    }) 
  },
  IDCard:function (e) {
    this.setData({
      IDCard:e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  uploadImg:function (e) {
    var that = this;
    var idx = e.currentTarget.dataset.idx;
    wx.chooseImage({
      sourceType: sourceType[2],
      sizeType: sizeType[2],
      count: 1,
      success: function (res) {
        const tempFilePaths = res.tempFilePaths[0];
        wx.showLoading({
          title: '图片上传中',
        })
        wx.uploadFile({
          url: app.data.api_url+'api/FJDispose/FJ_Upload_Img?Type=idcard&strrsa='+app.uploadImgStr(), 
          filePath: tempFilePaths,
          name: 'file',
          success (r){
            var rs = JSON.parse(r.data);
            if (rs.code == 200) {
              var arrayimg = 'imgs['+idx+'].img';
              var arrayisupload = 'imgs['+idx+'].isupload';
              var imgl = 'imgs['+idx+'].imgl';
              that.setData({
                [imgl]:tempFilePaths,
                [arrayimg]:rs.data,
                [arrayisupload]:true
              })
              wx.hideLoading();
            } 
          },
          fail(r){
            wx.hideLoading();
            app.tishi('图片上传失败,请检查网络');
          }
        })        
      }    
    })
  },
  FJ_Get_IDCardZjInfo:function () {
    if (app.checkFjLogin()) {
      var FjLoginLog = wx.getStorageSync('FjLoginLog'); 
      var api_input = {
        UID:FjLoginLog.UID,
      }
      var that = this;
      app.getDaoPostN('api/FJDispose/FJ_Get_IDCardZjInfo',api_input,r=>{
        
        if (r.code==200) {
          if (r.data.Info) {
            var IDCardFront = 'imgs[0].img';
            var IDCardBack = 'imgs[1].img';
            var IDCardHand = 'imgs[2].img';
            var IDCardFront_l = 'imgs[0].imgl';
            var IDCardBack_l = 'imgs[1].imgl';
            var IDCardHand_l = 'imgs[2].imgl';            
            var IDCardFrontisupload = 'imgs[0].isupload';
            var IDCardBackisupload = 'imgs[1].isupload';
            var IDCardHandisupload = 'imgs[2].isupload';
            that.setData({
              IDCard:r.data.Info.IDCard,
              IsUpdate:r.data.IsUpdate
            })
            if (r.data.Info.IDCardFront) {
              that.setData({
                [IDCardFront]:r.data.Info.IDCardFront,
                [IDCardFront_l]:r.data.Info.IDCardFront,
                [IDCardFrontisupload]:true
              })
            }
            if (r.data.Info.IDCardBack) {
              that.setData({
                [IDCardBack]:r.data.Info.IDCardBack,
                [IDCardBack_l]:r.data.Info.IDCardBack,
                [IDCardBackisupload]:true
              })
            }
            if (r.data.Info.IDCardHand) {
              that.setData({
                [IDCardHand]:r.data.Info.IDCardHand,
                [IDCardHand_l]:r.data.Info.IDCardHand,
                [IDCardHandisupload]:true
              })
            }
          }else{
            that.setData({
              IsUpdate:r.data.IsUpdate
            })
          }
        }
        
      })   
    }else{
      wx.navigateTo({ url: '../../pages/login/index' }); 
    } 
  },    
})